package com.recruit.g1.user.controller;


import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.common.response.StateCode;
import com.recruit.g1.pojo.passport.param.UserLoginParam;
import com.recruit.g1.pojo.user.dto.UserRegParam;
import com.recruit.g1.pojo.user.entity.User;
import com.recruit.g1.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Map;


@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/user")
public class UserController {


    @Autowired
    private UserService userService;//这个成员变量需要赋值一个UserService类型的对象 (用@Autowired从spring容器中找一个符合类型的对象赋值给userService成员变量)
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/test-redis")
    public String testRedis() {
        // 写入数据
        redisTemplate.opsForValue().set("test-key", "Hello Redis 7.4.4!");

        // 读取数据
        String value = redisTemplate.opsForValue().get("test-key");
        return "Redis 返回: " + value;
    }
    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam) {
        log.debug("开始查询是否存在用户"+userLoginParam);
        try {
            return userService.login(userLoginParam);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("报错");
            return JsonResult.fail("try运行报错");
        }
    }


    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public JsonResult register(@RequestBody UserRegParam userRegParam) {
        try {
            log.debug("controller注册try");
            return userService.register(userRegParam);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail("try运行报错");
        }
    }


    @ApiOperation(value = "密码重置")
    @PostMapping("/resetPassword")
    public JsonResult resetPassword(String password, @ApiIgnore HttpSession httpSession){
        System.out.println("controller重置密码"+password);
        try {
            return userService.resetPassword(password, httpSession);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(StateCode.OPERATION_FAILED);
        }

    }


    @ApiOperation(value = "获取当前状态")
    @GetMapping("/current")
    public JsonResult getCurrent(@ApiIgnore HttpSession httpSession){
        //////////////////////////////////////
        User userSaveParam = (User) httpSession.getAttribute("currentLoginUser");
        //UserSaveParam userSaveParam = (UserSaveParam) httpSession.getAttribute(httpSession.getId());

        if (userSaveParam==null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }

        return JsonResult.ok(userSaveParam);
    }

    @GetMapping("/info")
    public JsonResult getUserInfo(@RequestHeader("Authorization") String token) {
        // 假设格式是 Bearer <token>
        String actualToken = token.replace("Bearer ", "");

        // 调用服务层获取用户信息
        Map<String, Object> userInfo = userService.getUserInfoByToken(actualToken);

        if (userInfo != null) {
            return JsonResult.ok(userInfo);
        } else {
            return JsonResult.fail("用户未登录或Token已过期");
        }
    }
}

